package com.hspedu.homework;

import java.io.*;
import java.util.Properties;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/26
 * @time 19:47
 */
public class Homework03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        setProperties();
        Dog dog = new Dog();
        Properties properties = new Properties();
        properties.load(new FileReader("D:\\workspace_idea\\java0\\hspedu\\chapter19\\src\\dog.properties"));
        String name = properties.getProperty("name");
        String age = properties.getProperty("age");
        String color = properties.getProperty("color");

        dog.setName(name);
        dog.setAge(Integer.parseInt(age));
        dog.setColor(color);

        System.out.println(dog);

        serializeDog(dog);
        reverseSerialDog(dog);
    }

    public static void serializeDog(Dog dog) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\dog.dat"));

        oos.writeObject(dog);

        oos.close();

    }

    public static void reverseSerialDog(Object obj) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\dog.dat"));
        Object o = ois.readObject();
        System.out.println(o);
        ois.close();
    }

    public static void setProperties() throws IOException {
        String filePath = "D:\\workspace_idea\\java0\\hspedu\\chapter19\\src\\dog.properties";

        Properties properties = new Properties();
        properties.setProperty("name","汤姆");
        properties.setProperty("age","5");
        properties.setProperty("color","red");

        properties.store(new FileWriter(filePath),"dog类的相关配置");
    }
}

class Dog implements Serializable {
    private String name;
    private int age;
    private String color;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
